package com.kmxd.ams.controller.arrange;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.arrange.ICatalogSvc;
import com.kmxd.ams.client.arrange.dto.*;
import com.kmxd.ams.client.arrange.vo.CatalogListVO;
import com.kmxd.ams.client.arrange.vo.CatalogVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 目录树管理 前端控制器
 *
 * @author nickbi
 * @since 2022-08-04
 */
@RestController
@Tag(name = "目录树管理")
@RequestMapping("/arrange/catalog")
@AllArgsConstructor
public class CatalogCtl {

  private ICatalogSvc catalogService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<CatalogListVO>> selectPage(Page reqPage, CatalogQueryDTO req) {
    return R.success(catalogService.selectPage(reqPage, req));
  }

  /** 列表查询 */
  @Operation(summary = "列表查询")
  @GetMapping("/selectList")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "列表查询")
  public R<List<CatalogListVO>> selectList(CatalogQueryDTO req) {
    return R.success(catalogService.selectList(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody CatalogAddDTO req) {
    catalogService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody CatalogEditDTO req) {
    catalogService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @SaCheckPermission("arrange:catalog:remove")
  @AutoLog(
      module = "目录树管理",
      type = LogTypeEnum.ARCHIVE,
      level = LogLevelEnum.HIGH_RISK,
      title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    catalogService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "查看")
  public R<CatalogVO> view(@RequestParam Long id) {
    return R.success(catalogService.view(id));
  }

  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(CatalogQueryDTO req, HttpServletResponse response) {
    String filename = "目录树管理列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(CatalogListVO.class)
        .sheet("目录树管理列表")
        .doWrite(catalogService.selectList(req));
  }

  /** 查看 */
  @Operation(summary = "目录树")
  @PostMapping("/tree")
  @AutoLog(module = "目录树管理", type = LogTypeEnum.ARCHIVE, level = LogLevelEnum.LOW, title = "查看")
  public R<List<Tree<String>>> tree(@Validated @RequestBody CatalogTreeQueryDTO req) {
    return R.success(catalogService.tree(req));
  }

}
